﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using DGMResourcesTool.Logic.FileUtil;

namespace DGMResourcesTool.Logic.Config
{
    public class FilterConfigUtility
    {
        private static readonly List<string> FileList = new List<string>();
        private static readonly List<string> DirectoryList = new List<string>();
        private static readonly List<string> RegexList = new List<string>();
        /// <summary>
        /// 读入过滤文件.
        /// </summary>
        public static void ReadFilter()
        {
            try
            {
                FileList.Clear();
                DirectoryList.Clear();
                RegexList.Clear();
                string filterFileName = "filter.csv";
                string filterFilePath = FileUtility.GetConfigFilePath(filterFileName);
                if (string.IsNullOrEmpty(filterFilePath))
                {
                    LogUtility.AddToLog("配置文件不存在：" + filterFileName);
                    return;
                }

                string[] lines = FileReaderWriterUtility.ReadFile(filterFilePath, Encoding.Default);
                foreach (var line in lines)
                {
                    string[] strs = line.Split(',');
                    if (strs.Length > 0 && !string.IsNullOrEmpty(strs[0]))
                    {
                        FileList.Add(strs[0]);
                    }
                    if (strs.Length > 1 && !string.IsNullOrEmpty(strs[1]))
                    {
                        DirectoryList.Add(strs[1]);
                    }
                    if (strs.Length > 2 && !string.IsNullOrEmpty(strs[2]))
                    {
                        RegexList.Add(strs[2]);
                    }
                }
            }
            catch (Exception e)
            {
                LogUtility.AddToLog("强制过滤文件filter.txt不生效！！！");
                LogUtility.AddToLog(e.StackTrace);
                Console.WriteLine(e);
            }
        }

        /// <summary>
        /// 文件名过滤.
        /// </summary>
        /// <param name="str">文件名称,或者文件名称的子字符串</param>
        public static bool IsIgnoreFile(string str)
        {
            foreach (var filterKey in FileList)
            {
                if (filterKey.Contains(str))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 目录过滤.
        /// </summary>
        /// <param name="path">文件目录.</param>
        public static bool IsInIgnoreDirectory(string path)
        {
            foreach (var directory in DirectoryList)
            {
                if (path.Contains(directory))
                {
                    return true;
                }
            }
            return false;
        }

        public static bool IsIgnore(ResFileInfo resFileInfo)
        {
            if (!string.IsNullOrEmpty(resFileInfo.Name) && IsIgnoreFile(resFileInfo.Name))
            {
                return true;
            }

            if (!string.IsNullOrEmpty(resFileInfo.ProcessdName) && IsIgnoreFile(resFileInfo.ProcessdName))
            {
                return true;
            }

            if (IsInIgnoreDirectory(resFileInfo.FullPath))
            {
                return true;
            }

            // 检查名称是否符合正则.
            foreach (var regexConfigInfo in RegexList)
            {
                Match m5 = Regex.Match(resFileInfo.Name, regexConfigInfo);
                if (m5.Success)
                {
                    return true;
                }
                m5 = Regex.Match(resFileInfo.ProcessdName, regexConfigInfo);
                if (m5.Success)
                {
                    return true;
                }
            }

            return false;
        }
    }
}